package com.shuoyue.ycgk.ui.recruitment.resume.certificate;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.Cert;
import com.shuoyue.ycgk.entity.MajorSection;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;


public interface CertContract {
    interface View extends BaseView {
        void saveSuc();

        void querySuc(List<Cert> list);

        void deleteSuc(Cert cert);

    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void save(String name) {

            apply(model.save(name))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> recruitmentMainInfoOptional) {
                            super.onNext(recruitmentMainInfoOptional);
                            mView.saveSuc();
                        }
                    });
        }

        public void query() {

            apply(model.query())
                    .subscribe(new ApiSubscriber<Optional<List<Cert>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Cert>> recruitmentMainInfoOptional) {
                            super.onNext(recruitmentMainInfoOptional);
                            mView.querySuc(recruitmentMainInfoOptional.getIncludeNull());
                        }


                    });
        }

        public void delete(Cert certId) {

            apply(model.delete(certId.getId()))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> recruitmentMainInfoOptional) {
                            super.onNext(recruitmentMainInfoOptional);
                            mView.deleteSuc(certId);
                        }


                    });
        }

    }

    class Model {
        public Observable<BaseResult<String>> save(String name) {
            return RetrofitClient.getInstance().getApi().saveCert(name);
        }

        public Observable<BaseResult<List<Cert>>> query() {
            return RetrofitClient.getInstance().getApi().queryCert();
        }

        public Observable<BaseResult<String>> delete(int certId) {
            return RetrofitClient.getInstance().getApi().deleteCert(certId);
        }
    }
}